﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities.Messages;
using Forest.Core.Data;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Core;

namespace Forest.Cat.Domain.Services.Messages
{
    public class ReceivedMessageService:IReceivedMessageService
    {
                #region Constants
        private const string ReceivedMessage_BY_ID_KEY = "receivedMessage.id-{0}";
        private const string ReceivedMessage_PATTERN_KEY = "receivedMessage.";
        #endregion

        #region Fields
        private readonly IRepository<ReceivedMessage> _receivedMessageRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;

        #endregion

        #region Ctor
        public ReceivedMessageService(IRepository<ReceivedMessage> workExperienceRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _receivedMessageRepository = workExperienceRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteReceivedMessage(ReceivedMessage receivedMessage)
        {
            if (receivedMessage == null)
                throw new ArgumentNullException("ReceivedMessage");

            _receivedMessageRepository.Delete(receivedMessage);

            _cacheManager.RemoveByPattern(ReceivedMessage_PATTERN_KEY);
            //event notification
            _eventPublisher.EntityDeleted(receivedMessage);

        }

        public ReceivedMessage GetReceivedMessageById(Guid receivedMessageId)
        {
            if (receivedMessageId == Guid.Empty)
                return null;

            string key = string.Format(ReceivedMessage_BY_ID_KEY, receivedMessageId);

            return _cacheManager.Get(key, () =>
            {
                var cl = _receivedMessageRepository.GetById(receivedMessageId);
                return cl;
            });
        }

        public virtual IPagedList<ReceivedMessage> GetAllReceivedMessages(int pageIndex, int pageSize)
        {
            var query = _receivedMessageRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<ReceivedMessage>(query, pageIndex, pageSize);
            return items;
        }

        public virtual void InsertReceivedMessage(ReceivedMessage receivedMessage)
        {
            if (receivedMessage == null)
                throw new ArgumentNullException("ReceivedMessage");

            _receivedMessageRepository.Insert(receivedMessage);

            _cacheManager.RemoveByPattern(ReceivedMessage_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(receivedMessage);
        }

        public virtual void UpdateReceivedMessage(ReceivedMessage receivedMessage)
        {
            if (receivedMessage == null)
                throw new ArgumentNullException("ReceivedMessage");
            _receivedMessageRepository.Update(receivedMessage);

            _cacheManager.RemoveByPattern(ReceivedMessage_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(receivedMessage);
        }
        #endregion

        public IQueryable<ReceivedMessage> GetReceivedMessagesByJobseekerId(Guid id)
        {
            return _receivedMessageRepository.Table.Where(w => w.To == id);
        }
    }
}
